//
//  DJMainViewController.swift
//  大江微博
//
//  Created by 刘文江 on 2016/12/5.
//  Copyright © 2016年 刘文江. All rights reserved.
//

import UIKit

//主控制器

//通过反射机制，来创建子控制器
class DJMainViewController: UITabBarController {

    //MARK: 私有控件
    //懒加载按钮
    lazy var composeButton:UIButton = {
        let button = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        
        return button!
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        
        tabBar.addSubview(composeButton)
        
        //设置按钮的位置
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        
        let width = tabBar.bounds.width / count - 1
        
        //insetBy缩进
        composeButton.frame = tabBar.bounds.insetBy(dx: width * 2, dy: 1)
        
        //为按钮添加方法
        composeButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        
    }
    
    //按钮方法
    func buttonAction() -> Void {
        print("撰写微博")
        
    }

}

//extension  类似于OC的分类，在Swift中还可以用来切分代码
//可以把相近功能的函数，放在一个extension钟
//便于代码维护
//注意：和OC的分类一样，extension中不能定义属性，只能定义方法

//MARK: - 设置界面
extension DJMainViewController{
    
//    //设置撰写按钮
//    func setupComposeButton() {
//        tabBar.addSubview(composeButton)
//    }
    //设置所有的子控制器
    func setupChildControllers(){
        
        let array = [
            ["clsName":"DJHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"DJMessageViewController","title":"消息","imageName":"message"],
            ["clsName":"UIViewController"],
            ["clsName":"DJDiscoverViewController","title":"发现","imageName":"discover"],
            ["clsName":"DJProfileViewController","title":"个人","imageName":"profile"]
        ]
        
        var arrayM = [UIViewController]()
        
        for dict in array {
            let myvc = controller(dict: dict)
            
            arrayM.append(myvc)
        }
        
        viewControllers = arrayM
        
        
    }
    
    //使用字典创建一个子控制器
    func controller(dict:[String:String]) -> UIViewController {
        
        //取得字典的内容
        guard let clsName = dict["clsName"],let title = dict["title"],let imageName = dict["imageName"],let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else{
            return UIViewController()
        }


        //创建视图控制器
        let vc = cls.init()
        
        //设置标题
        vc.title = title
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabbar的标题字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        
        //设置tabbar的字体大小 , 系统默认是12号字 ，而且要在normal状态下设置，高亮状态是不管用的
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for: .normal)
        
        let nav = DJNavgationController(rootViewController: vc)
        
        return nav
   
    }
}
